<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>简易手机通讯录</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 600px;
            margin: 0 auto;
            padding: 20px;
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: inline-block;
            width: 80px;
        }
        input[type="text"] {
            padding: 8px;
            width: 200px;
        }
        button {
            padding: 8px 15px;
            margin-right: 10px;
            cursor: pointer;
        }
        textarea {
            width: 100%;
            height: 200px;
            padding: 8px;
            margin-top: 20px;
        }
    </style>
</head>
<body>
    <h1>简易手机通讯录</h1>

    <div class="form-group">
        <label for="name">姓名:</label>
        <input type="text" id="name" placeholder="请输入姓名">
    </div>

    <div class="form-group">
        <label for="phone">电话:</label>
        <input type="text" id="phone" placeholder="请输入电话">
    </div>

    <div class="button-group">
        <button id="add">添加</button>
        <button id="search">查询</button>
        <button id="delete">删除</button>
        <button id="reset">重置</button>
    </div>

    <textarea id="contactList" readonly></textarea>

    <script>
        $(document).ready(function() {
            updateContactList();
            $('#add').click(function() {
                const name = $('#name').val().trim();
                const phone = $('#phone').val().trim();

                if (!name || !phone) {
                    alert('姓名和电话不能为空！');
                    return;
                }
                let contacts = JSON.parse(localStorage.getItem('phoneContacts')) || {};
                contacts[name] = phone;
                localStorage.setItem('phoneContacts', JSON.stringify(contacts));
                updateContactList();
                alert('联系人添加成功！');
            });
            $('#search').click(function() {
                const name = $('#name').val().trim();

                if (!name) {
                    alert('请输入要查询的姓名！');
                    return;
                }
                const contacts = JSON.parse(localStorage.getItem('phoneContacts')) || {};

                if (contacts[name]) {
                    $('#phone').val(contacts[name]);
                } else {
                    alert('未找到该联系人！');
                }
            });
            $('#delete').click(function() {
                const name = $('#name').val().trim();

                if (!name) {
                    alert('请输入要删除的姓名！');
                    return;
                }
                let contacts = JSON.parse(localStorage.getItem('phoneContacts')) || {};

                if (contacts[name]) {
                    delete contacts[name];
                    localStorage.setItem('phoneContacts', JSON.stringify(contacts));
                    updateContactList();
                    $('#name').val('');
                    $('#phone').val('');
                    alert('联系人删除成功！');
                } else {
                    alert('未找到该联系人！');
                }
            });
            $('#reset').click(function() {
                $('#name').val('');
                $('#phone').val('');
            });
            function updateContactList() {
                const contacts = JSON.parse(localStorage.getItem('phoneContacts')) || {};
                let contactText = '';

                for (const name in contacts) {
                    contactText += `${name}: ${contacts[name]}\n`;
                }

                $('#contactList').val(contactText);
            }
        });
    </script>
</body>
</html>